# coding=utf-8
# 全世界最帅的帅哥
from flask import request, g
from flask_restful import reqparse

from ysgg1_club_comment.modules.subject_info import SubjectInfo
from ysgg1_club_comment.modules.subject_liked import SubjectLiked
from ysgg1_club_subject.comment.enums.subject_liked_status_enum import SubjectLikedStatusEnum
from ysgg1_club_subject.constant.redis_prefix import RedisPrefix
from ysgg1_club_subject.redis_client import redis_liked_client

from ysgg1_club_subject.subject_serializers.subject_liked_serializers import SubjectLikedSerializer


def build_key(subject_id, liked_user_id):
    return str(subject_id) + ":" + str(liked_user_id)


def create_liked():
    """
    新增一个点赞数据
    :return:
    """
    rp = reqparse.RequestParser()
    rp.add_argument('subjectId', required=True, help='SubjectId is required')
    rp.add_argument('status', required=True, help='点赞状态不能为空')
    args = rp.parse_args()
    subject_id = args.subjectId
    status = args.status

    openid = g.token
    hash_key = build_key(subject_id, openid)
    redis_liked_client.hset(RedisPrefix.SUBJECT_LIKED_KEY, hash_key, status)
    count_key = RedisPrefix.SUBJECT_LIKED_COUNT_KEY + "." + subject_id
    detail_key = RedisPrefix.SUBJECT_LIKED_DETAIL_KEY + "." + subject_id + "." + openid
    if SubjectLikedStatusEnum.liked.code == status:
        redis_liked_client.incrby(count_key, 1)
        redis_liked_client.set(detail_key, "1")
    else:
        count = redis_liked_client.get(count_key)
        if not count:
            return {"code": 200, "message": "没点赞前不可以取消点赞"}
        count = int(count.decode("utf8"))
        if count < 0:
            return
        redis_liked_client.incrby(count_key, -1)
        redis_liked_client.delete(detail_key)


def is_liked(subject_id, user_id):
    """
    是否点赞过
    :param subject_id: subject_id
    :param user_id: user的openid
    :return: Boolean
    """
    detail_key = RedisPrefix.SUBJECT_LIKED_DETAIL_KEY + "." + subject_id + "." + user_id
    return bool(redis_liked_client.exists(detail_key))


def get_liked_count(subject_id):
    """
    获取题目点赞数量
    :param subject_id:
    :return:
    """
    count_key = RedisPrefix.SUBJECT_LIKED_COUNT_KEY + "." + subject_id
    count = redis_liked_client.get(count_key)
    if not count:
        return 0
    count = int(count.decode("utf8"))
    if count < 0:
        return 0
    return count


def get_subject_liked_page():
    """
    查询我的点赞列表
    :return:
    """
    request_data = request.get_json()
    page = request_data.get("page", 1)
    page_size = request_data.get("pageSize", 10)

    subject_liked_page = SubjectLiked.query.filter_by(like_user_id=g.token, is_delete=0, status=1).paginate(
        page=page, per_page=page_size, max_per_page=20, error_out=False)

    for subject_like in subject_liked_page:
        subject_info = SubjectInfo.query.filter_by(id=subject_like.subject_id).with_entities(
            SubjectInfo.subject_name).first()
        subject_like.subject_name = subject_info.subject_name

    data = SubjectLikedSerializer(subject_liked_page).to_dict()
    return data
